/*
 * @Author: HeAo
 * @Date: 2021-10-25 20:48:37
 * @LastEditTime: 2021-10-26 16:56:29
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \ej-customer\src\store\shopcart\shopcart.js
 * 别乱动！
 */
import _ from 'lodash'
export default {
  namespaced: true,
  state () {
    return {
      orderLines: new Map()
    }
  },
  getters: {
    total (state) {
      let total = 0;
      Array.from(state.orderLines.values()).forEach(item => {
        total += item.price * item.number
      })
      return total
    }
  },
  mutations: {
    addShopCar (state, orderLine) {
      state.orderLines.set(orderLine.id, orderLine)
      // vue监听不到引用属性类型内部值的改变。只能监听到引用地址的变化
      // 解决方法：使用克隆的方法，强行改变引用地址，能够让vue监听到数据的改变
      state.orderLines = _.clone(state.orderLines)
    },
    // 清空购物车
    clearShopCar (state) {
      state.orderLines.clear()
      state.orderLines = _.clone(state.orderLines)
    }
  }
}